package com.ctshk.rpc.scenic.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.rpc.scenic.dto.*;
import com.ctshk.rpc.scenic.entity.*;
import com.ctshk.rpc.scenic.mapper.*;
import com.ctshk.rpc.scenic.req.*;
import com.ctshk.rpc.scenic.service.IScenicProductCostStrategySpecAttrService;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * <p>
 *
 * </p>
 *
 * @author chenlei
 * @since 2/2/2021
 */
@DubboService
public class ScenicProductCostStrategySpecAttrServiceImpl extends ServiceImpl<ScenicProductCostStrategySpecAttrMapper, ScenicProductCostStrategySpecAttr> implements IScenicProductCostStrategySpecAttrService {

    @Autowired
    ScenicProductCostStrategySpecAttrMapper scenicProductCostStrategySpecAttrMapper;

    @Autowired
    ScenicProductTicketMapper scenicProductTicketMapper;


    @Override
    public Result<List<ScenicProductCostStrategySpecAttrDTO>> specAttrName(ScenicProductTicketDetailReq scenicProductTicketDetailReq) {
        ScenicProductTicket scenicProductTicket = scenicProductTicketMapper.selectById(scenicProductTicketDetailReq.getId());
        List<ScenicProductCostStrategySpecAttrDTO> strategySpecAttrDTOS = new ArrayList<>();
        if(scenicProductTicket != null){
            QueryWrapper<ScenicProductCostStrategySpecAttr> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("is_deleted",0);
            queryWrapper.eq("product_id",scenicProductTicket.getProductId());
            List<ScenicProductCostStrategySpecAttr> list = scenicProductCostStrategySpecAttrMapper.selectList(queryWrapper);
            if(CollectionUtils.isNotEmpty(list)){
                for(ScenicProductCostStrategySpecAttr scenicProductCostStrategySpecAttr : list){
                    if(scenicProductCostStrategySpecAttr.getAttributeName().equals(scenicProductTicket.getAttr1Name())){
                        ScenicProductCostStrategySpecAttrDTO scenicProductCostStrategySpecAttrDTO = new ScenicProductCostStrategySpecAttrDTO();
                        scenicProductCostStrategySpecAttrDTO.setSpecName(scenicProductCostStrategySpecAttr.getSpecName());
                        scenicProductCostStrategySpecAttrDTO.setAttributeName(scenicProductCostStrategySpecAttr.getAttributeName());
                        strategySpecAttrDTOS.add(scenicProductCostStrategySpecAttrDTO);
                    }else if(scenicProductCostStrategySpecAttr.getAttributeName().equals(scenicProductTicket.getAttr2Name())){
                        ScenicProductCostStrategySpecAttrDTO scenicProductCostStrategySpecAttrReqDTO = new ScenicProductCostStrategySpecAttrDTO();
                        scenicProductCostStrategySpecAttrReqDTO.setSpecName(scenicProductCostStrategySpecAttr.getSpecName());
                        scenicProductCostStrategySpecAttrReqDTO.setAttributeName(scenicProductCostStrategySpecAttr.getAttributeName());
                        strategySpecAttrDTOS.add(scenicProductCostStrategySpecAttrReqDTO);
                    }
                }
            }
        }
        return Result.success(strategySpecAttrDTOS);
    }

    @Override
    public Result updateStock(Long stockId, Integer number) {

        return Result.success(scenicProductCostStrategySpecAttrMapper.updateStock(stockId,number));
    }

    @Override
    public Result updateAddStock(Long stockId, Integer number) {
        return Result.success(scenicProductCostStrategySpecAttrMapper.updateAddStock(stockId,number));
    }
}
